import Qt 4.7

Item {
    id: tabWidget
    property alias tabModel:tabModelS
    property alias tabHeader:header
    property int current: 0
    onCurrentChanged: setVisible()
    signal tabClosed(string ip)
    ListModel
    {
        id:tabModelS
    }

    Row
    {
        id: header

        Repeater
        {
            model: tabModel
            delegate:
                Rectangle
                {
                    width: tabWidget.width / tabModel.count; height: 36

                    Rectangle
                    {
                        width: parent.width; height: 1
                        anchors { bottom: parent.bottom; bottomMargin: 1 }
                        color: "#acb2c2"
                    }
                    BorderImage
                    {
                        anchors { fill: parent; leftMargin: 2; topMargin: 5; rightMargin: 1 }
                        border { left: 7; right: 7 }
                        source: "images/tab.png"
                        visible: tabWidget.current == index
                    }
                    Text
                    {
                        horizontalAlignment: Qt.AlignHCenter; verticalAlignment: Qt.AlignVCenter
                        anchors.fill: parent
                        text: tabModel.get(index).nickyname
                        elide: Text.ElideRight
                        font.bold: tabWidget.current == index
                    }
                    MouseArea
                    {
                        anchors.fill: parent
                        onClicked: tabWidget.current = index
                    }
                }

        }
    }

    function setVisible()
    {
        for (var i = 0; i < tabModel.count; ++i)
        {
            tabModel.get(i).chatTab.visible = (i == current ? 1 : 0)
        }
    }
    function closeCurrentTab()
    {
        if(tabModel.count>=1 && current!=0)
        {
           var tab=tabModel.get(current).chatTab
           var ip = tabModel.get(current).ip
           tabModel.remove(current);
           current = 0;
           tab.destroy()
           tabClosed(ip) // signal
         }
    }

    function addChatTab(ip,name)
    {
        console.log("try add tab",ip,"  ",name)
        for(var i=0;i<tabModel.count;i++)
        {
           if(tabModel.get(i).ip == ip)
           {
               if(tabModel.get(i).name != name)
                    tabModel.get(i).name = name;
               console.log("already exist")
               current = i
               return null;
           }
        }
        // where to store the componet global,headache
        var component = Qt.createComponent("ChatTab.qml");
        var object = component.createObject(tabwidget);
        object.ip=ip
        addTab(ip,name,object)
        return object
    }

    function addTab(ip,name,tab)
    {
        tabModel.append({"ip":ip,"nickyname":name,"chatTab":tab});
        current = tabwidget.tabModel.count -1
    }

    function getTab(ip)
    {
        console.log("get tab",ip,tabModel.count)
        for(var i=0;i<tabModel.count;i++)
        {
           if(tabModel.get(i).ip == ip)
           {
               return tabModel.get(i).chatTab
           }
        }
        return null
    }


}
